#include <Arduino.h>

#include "bsp_i2s.h"


/**
 * @brief 配置 i2s 引脚
 * 
 */
void i2s_setpin()
{      
  const i2s_pin_config_t pin_config = {     
      .bck_io_num = I2S_SCK,
      .ws_io_num = I2S_WS,
      .data_out_num = I2S_PIN_NO_CHANGE,
      .data_in_num = I2S_SD};

  esp_err_t err = i2s_set_pin(I2S_PORT_0, &pin_config);
  if (err != ESP_OK)
  {
    Serial.printf("I2S set pin failed (I2S_PORT_0): %d\n", err);
    while (true)
      ;
  }
  else
  {
    Serial.printf("I2S set pin OK!\r\n");
  }
}


/**
 * @brief 配置 i2s 参数
 * 
 */
void i2s_install()
{
  const i2s_config_t i2s_config = {
      .mode = (i2s_mode_t)(I2S_MODE_MASTER | I2S_MODE_RX),
      .sample_rate = SAMPLE_RATE,
      .bits_per_sample = i2s_bits_per_sample_t(16),
      .channel_format = I2S_CHANNEL_FMT_ONLY_LEFT,
      .communication_format = i2s_comm_format_t(I2S_COMM_FORMAT_STAND_I2S),
      .intr_alloc_flags = 0, // default interrupt priority
      .dma_buf_count = 8,
      .dma_buf_len = 1024,
      .use_apll = false};

  esp_err_t err = i2s_driver_install(I2S_PORT_0, &i2s_config, 0, NULL);
  if (err != ESP_OK)
  {
    Serial.printf("I2S driver install failed (I2S_PORT_0): %d\n", err);
    while (true)
        ;
  }
  else
  {
    Serial.printf("I2S driver install OK\r\n");
  }
}







